package
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class QuadTreeSpatialIndex implements ISpatialIndex
	{
		public var root:QuadTreeNode;
		private var m_objects:Vector.<ISpatialIndexable>;
		
		public function QuadTreeSpatialIndex(dimensions:Point)
		{
			m_objects = new Vector.<ISpatialIndexable>;
			root = new QuadTreeNode(new Rectangle(0,0,dimensions.x, dimensions.y));
		}
		
		public function getDisplay():Sprite{
			var disp:Sprite = root.getDisplay();
			return disp;
		}

		public function add(entity:ISpatialIndexable):void{
			root.add(entity,0);
		}
		
		public function remove(entity:ISpatialIndexable):void{
			root.remove(entity,0);
		}
		
		public function find(entity:ISpatialIndexable):Boolean{
			return root.find(entity);
		}
		
		public function getPointCollisions(pnt:Point):Vector.<ISpatialIndexable>{
			var collisions:Vector.<ISpatialIndexable> = new Vector.<ISpatialIndexable>;
			root.getPointCollisions(pnt, collisions);
			return collisions;
		}
		
		public function getRectCollisions(rect:Rectangle):Vector.<ISpatialIndexable>{
			var collisions:Vector.<ISpatialIndexable> = new Vector.<ISpatialIndexable>;
			root.getRectCollisions(rect, collisions);
			return collisions;		
		}
	}
}